<template>
  <div class="container">
    <div class="top">
      <div class="content">
        <div class="icons">￥</div>
        <div class="tits" style="margin-top: 20px;">
          <span>总余额:</span>
          <span class="balance">￥{{ userInfo?.zyeBalance | yuan }}</span>
          <!-- <span>可提现余额:</span>
            <span class="balance " style="color: green;margin-top: 10px;text-align: center">￥{{ userInfo?.ktxBalance | yuan }}</span> -->
        </div>
        <div class="centers">

          <div class="topss">
            <span>交易中余额:</span>
            <span class="balances ">￥{{ userInfo?.jyzBalance | yuan }}</span>
          </div>
          <div class="topss" >
            <span>可抵扣余额:</span>
            <span class="balances ">￥{{ userInfo?.kdkBalance  | yuan }}</span>
          </div>
          
          <div class="tops">
            <div class="left">
              <span>可提现余额:</span>
              <span class="balance " style="color: green;margin-top: 10px;text-align: center">￥{{ userInfo?.ktxBalance | yuan }}</span>
            </div>

            <div class="btns">
              <div class="btn ti" @click="shows('wthdrawal')">
                <span>提现</span>
              </div>
            </div>
            <!-- <span>总余额:</span>
              <span class="balances">￥{{ userInfo?.zyeBalance | yuan }}</span> -->
          </div>
        </div>

      </div>


      <div class="tips">
        <div>*提现手续费为0.4%</div>
        <div>*到账时间为次日15:00</div>
      </div>
    </div>

    <van-dialog v-model="showWthdrawal" title="提现" show-cancel-button @cancel="() => this.showWthdrawal = false"
      confirm-button-text="立即提现" @confirm="confirmPay">
      <div class="selected">
        <div class="wthdrawal">
          <div class="bttn">
            <div class="all" :class="{
              selecteds: setWthdrawals
            }" @click="setWthdrawal">
              <span>全部</span>
            </div>
            <div class="btn inputss">
              <input v-model="wthdrawalCount" class="inputs" type="number" placeholder="自定义金额" @focus="closeWthdrawals">
            </div>
          </div>
          <div  v-if="this.setWthdrawals == true || (this.wthdrawalCount!='' && this.wthdrawalCount !=0 )">
            <p style="line-height: 60px;">提现手续费：<span style="color:#ef3939;"> 0.4% </span></p>
            <p>实际到账金额：<span style="color:#ef3939;">￥{{ actualReceivedAmount }}</span></p>
          </div>
        </div>
      </div>
    </van-dialog>
  </div>
</div></template>
<script>
import { getBalance, withdrawalReview, getInfo } from '@/api/user'

import { mapGetters } from 'vuex'
export default {
  name: 'Balance',
  computed: {
  },
  props: ['userInfo'],
  data() {
    return {
      nowBalbance: 0,
      withdrawalAmount: 0,
      rechargeList: ['50.00', '100.00', '500.00', '1000.00'],
      rechargeCount: '',
      wthdrawalCount: '',
      show: '',
      setWthdrawals: false,
      showRecharge: false,
      selectIndex: 0,
      showWthdrawal: false,
      ispay: false,
    }
  },
  computed:{
    commission(){
      if(this.setWthdrawals){
        return (Number.parseFloat(this.userInfo.ktxBalance)*0.004).toFixed(2)
      }else{
        return (Number.parseFloat(this.wthdrawalCount)*0.004).toFixed(2)
      }
    },
    actualReceivedAmount(){
      if(this.setWthdrawals){
        return (Number.parseFloat(this.userInfo.ktxBalance)*0.996).toFixed(2)
      }else{
        return (Number.parseFloat(this.wthdrawalCount)*0.996).toFixed(2)
      }
    }
  },
  async created() {
    // await this.getInfos()
  },
  methods: {
    shows(show) {
      // this.setWthdrawals = false
      if (this.userInfo.ktxBalance === 0) {
        return this.$toast.fail('暂无可提现金额')
      }
      if (show === 'wthdrawal') {
        this.showWthdrawal = true
      } else {
        this.showRecharge = true
      }
      // this.showRecharge = true
      this.show = show
    },
    selects(val, index) {
      // this.rechargeCount = val
      this.selectIndex = index
    },
    setWthdrawal() {
      this.setWthdrawals = true
      // this.wthdrawalCount = this.userInfo.ktxBalance
    },
    async confirmPay() {
      this.$toast.loading('加载中')
      if (!this.ispay) {
        this.ispay = true
      } else {
        this.ispay = false
        return false
      }
      const data = {
        userRealName: this.userInfo.userRealName,
        id: this.userInfo.id,
        userWithdrawalAmount: this.userInfo.ktxBalance,
        mobile: this.userInfo.mobile
      }
      // console.log(this.userInfo)
      if (this.setWthdrawals !== true) {
        data.userWithdrawalAmount = this.wthdrawalCount
        if (this.wthdrawalCount > this.userInfo.ktxBalance) {
          this.withdrawalAmount = ''
          this.showWthdrawal = false

          this.$toast.fail('填入金额过大')
          return false
        }
      }
      if (data.userWithdrawalAmount < 100) {
        return this.$toast.fail('每次提现金额应不低于100元')
      }
      data.commission=this.commission
      data.actualReceivedAmount = this.actualReceivedAmount
      const res = await withdrawalReview(data)
      if (res.code === 200) {
        this.$toast.success('申请成功')
        this.$emit('refresh')
        this.wthdrawalCount = ''
      }

      // await this.getInfos()
      this.ispay = false
      console.log(res)
    },
    closeWthdrawals() {
      this.wthdrawalCount = ''
      this.setWthdrawals = false
    },
  }
}
</script>
<style lang='scss' scoped>
.container {
  margin: 0;
  padding: 0;

  .top {
    margin: 0 24px;
    padding-top: 100px;

    .content {
      display: flex;
      flex-direction: column;
      align-items: center;

      .icons {
        width: 100px;
        height: 100px;
        line-height: 100px;
        border-radius: 50%;
        text-align: center;
        font-size: 60px;
        color: #fff;
        background-color: yellow;

      }

      .tits {
        display: flex;
        flex-direction: column;
        font-size: 30px;
        line-height: 40px;
        font-weight: bold;
        align-items: center;

        .balance {
          color: #ef3939;
          line-height: 40px;
          margin-top: 20px;
          // margin-left: 30px
        }
      }

      .centers {
        display: flex;
        flex-direction: column;
        box-sizing: border-box;
        // padding: 0 40px;
        height: 40px;
        line-height: 40px;
        width: 100%;
        justify-content: space-between;
        align-items: flex-start;
        margin-top: 60px;
        .topss{
          line-height: 80px;
        }
        .tops {
          width: 100%;
          line-height: 80px;
          display: flex;
          flex-direction: row;
          align-items: center;
          justify-content: space-between;

          .btns {
            // position: fixed;
            // bottom: 400px;
            display: flex;
            align-items: center;
            justify-content: space-evenly;

            .btn {
              width: 230px;
              height: 80px;
              line-height: 80px;
              border-radius: 20px;
              text-align: center;
              // margin-top: 150px;
            }

            .chong {
              color: #ef3939;
              border: 1px solid #ef3939;
            }

            .ti {
              color: green;
              // margin-left: 100px;
              border: 1px solid green;
            }
          }
        }

        .balances {
          color: #ef3939;
          // margin-left: 30px
        }
      }



    }



    .tips {
      position: fixed;
      bottom: 150px;
      width: 100%;
      text-align: center;
      font-size: 20px;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
    }
  }

}

.selected {
  margin-top: 100px;
  padding: 0 24px;
  margin-bottom: 100px;

  .recharge {
    display: flex;
    flex-direction: column;
    align-items: center;

    .list {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      justify-content: flex-start;

      .btn {
        margin-top: 20px;
        margin-left: 40px;
        width: 190px;
        height: 80px;
        line-height: 80px;
        border: 1px solid #00000080;
        border-radius: 20px;
        text-align: center;
      }

      ::v-deep(input) {
        width: 180px !important;
        line-height: 70px !important;
        border: none !important;
        padding: 0 !important;
        margin: 0;
        text-align: center;
      }

      .inputss {
        margin-left: 40px;
      }

      .notFirst {
        margin-left: 60px
      }
    }

    .paybtn {
      width: 230px;
      height: 80px;
      line-height: 80px;
      text-align: center;
      border-radius: 20px;
      color: #ef3939;
      margin-top: 50px;
      border: 1px solid #ef3939;
    }

    .selects {
      color: #ef3939;
      border: 1px solid #ef3939 !important;
    }
  }
}

.wthdrawal {
  margin-top: 100px;
  display: flex;
  flex-direction: column;
  align-items: center;

  .bttn {
    display: flex;
    flex-direction: row;
    align-items: center;

    .all {
      width: 200px;
      height: 80px;
      line-height: 80px;
      text-align: center;
      border: 1px solid #00000050;
      border-radius: 20px;
    }

    .btn {
      // margin-top: 20px;
      margin-left: 20px;
      width: 190px;
      height: 80px;
      line-height: 80px;
      border: 1px solid #00000080;
      border-radius: 20px;
      text-align: center;
    }

    ::v-deep(input) {
      width: 180px !important;
      line-height: 70px !important;
      border: none !important;
      padding: 0 !important;
      margin: 0;
      text-align: center;
    }

    .inputss {
      margin-left: 20px;
    }
  }

  .wthdrawalbtn {
    width: 230px;
    height: 80px;
    line-height: 80px;
    text-align: center;
    border-radius: 20px;
    border: 1px solid green;
    color: green;
    margin-top: 50px;
  }

}

.selecteds {
  border: 1px solid green !important;
}
</style>